﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Ebada.Platform.Model;
using Ebada.UI.Base;
using Ebada.Client;
using Ebada.Core;
using Ebada.Client.Platform;
namespace Ebada.Modules.Vehicle
{
    /// <summary>
    /// 违章记录编辑
    /// </summary>
    public partial class frmVehicleRefuelEdit : FormBase, IPopupFormEdit {

        decimal cardval = 0;
        public frmVehicleRefuelEdit() {
            InitializeComponent();
        }
        void dataBind() {

            this.txtVehicleNumber.DataBindings.Add("EditValue", rowData, "VehicleNumber");
            this.txtFillDate.DataBindings.Add("EditValue", rowData, "FillDate");
            this.txtAct_Code.DataBindings.Add("EditValue", rowData, "Act_Code");
            this.txtAct_Name.DataBindings.Add("EditValue", rowData, "Act_Name");
            this.txtAccountCode.DataBindings.Add("EditValue", rowData, "AccountCode");
            this.txtAccountTitle.DataBindings.Add("EditValue", rowData, "AccountTitle");
            this.lookGasCard_Num.DataBindings.Add("EditValue", rowData, "GasCard_Num");
            this.lookGasStation.DataBindings.Add("EditValue", rowData, "GasStation");
            this.txtNumber.DataBindings.Add("EditValue", rowData, "Number");
            this.txtPrice.DataBindings.Add("EditValue", rowData, "Price");
            this.txtTotalPrice.DataBindings.Add("EditValue", rowData, "TotalPrice");
            this.txtKmNumber.DataBindings.Add("EditValue", rowData, "KmNumber");
            this.txtRefuelType.DataBindings.Add("EditValue", rowData, "RefuelType");
            this.txtGasolineModel.DataBindings.Add("EditValue", rowData, "GasolineModel");
            this.txtBOrgcnName.DataBindings.Add("EditValue", rowData, "BOrgcnName");
            this.txtEmployeeName.DataBindings.Add("EditValue", rowData, "EmployeeName");
            this.txtRemark.DataBindings.Add("EditValue", rowData, "Remark");
            
        }
        #region IPopupFormEdit Members
        private VehicleRefuel rowData = null;

        public object RowData {
            get {
                return rowData;
            }
            set {
                if (value == null) return;
                if (rowData == null) {
                    this.rowData = value as VehicleRefuel;
                    dataBind();
                    InitComboBoxData();
                } else {
                    ConvertHelper.CopyTo<VehicleRefuel>(value as VehicleRefuel, rowData);
                    InitComboBoxData();
                }
            }
        }

        #endregion

        private void InitComboBoxData() {
            IList<Activities> list = MainHelper.GetSqlMap<Activities>().GetListByWhere<Activities>(" where ProjectName='车辆项目' and Act_Name='加油'");
            if (list != null && list.Count > 0)
            {
                rowData.Act_Code = list[0].Act_Code;
                rowData.Act_Name = list[0].Act_Name;
            }
            IList<Vehicles> vlist = MainHelper.GetSqlMap<Vehicles>().GetListByWhere<Vehicles>(" order by VehicleNumber");
            txtVehicleNumber.Properties.DataSource = vlist;
            IList<Accounts> acclist = MainHelper.GetSqlMap<Accounts>().GetListByWhere<Accounts>(" where DebitOrCredit like '%行政%' order by AccountCode");
            txtAccountTitle.Properties.DataSource = acclist;
            IList<VehicleGasCard> Gaslist = MainHelper.GetSqlMap<VehicleGasCard>().GetListByWhere<VehicleGasCard>(" order by GasCard_Num");
            lookGasCard_Num.Properties.DataSource = Gaslist;
            IList<VehicleGasStation> Stalist = MainHelper.GetSqlMap<VehicleGasStation>().GetListByWhere<VehicleGasStation>("  order by GasStation");
            lookGasStation.Properties.DataSource = Stalist;
            Vehicle_Common vc = new Vehicle_Common();
            vc.Add_DIC("Gasoline", txtGasolineModel);

            if (rowData.VehicleNumber=="")
            {
                rowData.FillDate = DateTime.Now;
            }
        }
 
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtVehicleNumber.Text == "")
            {
                MessageBox.Show("车牌号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtVehicleNumber.Focus();
                return;
            }
            if (txtAccountTitle.Text == "")
            {
                MessageBox.Show("科目名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtAccountTitle.Focus();
                return;
            }
            if (lookGasCard_Num.Text == "")
            {
                MessageBox.Show("油卡卡号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                lookGasCard_Num.Focus();
                return;
            }
            if (lookGasStation.Text == "")
            {
                MessageBox.Show("油站名称不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                lookGasStation.Focus();
                return;
            }
            if (txtGasolineModel.Text == "")
            {
                MessageBox.Show("汽油型号不能为空。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtGasolineModel.Focus();
                return;
            }
          
            IList<VehicleGasCard> cardlist = MainHelper.GetSqlMap<VehicleGasCard>().GetListByWhere<VehicleGasCard>(" where GasCard_Num='" + lookGasCard_Num.Text + "'");
            if (cardlist.Count > 0)
            {
                IList<VehicleRefuel> vrlist = MainHelper.PlatformSqlMap.GetListByWhere<VehicleRefuel>(" where VR_ID='" + rowData.VR_ID + "'");
               if (vrlist.Count > 0)
               {
                  
                   VehicleRefuel vr = vrlist[0];
                   VehicleGasCard card = cardlist[0];
                   card.TatalPrice = card.TatalPrice + vr.TotalPrice - txtTotalPrice.Value;
                   if (txtTotalPrice.Value > card.TatalPrice)
                   {
                       MessageBox.Show("此卡中余额不足。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       return;
                   }
                   MainHelper.GetSqlMap<VehicleGasCard>().Update<VehicleGasCard>(card);
               }
               else
               {
                   if (txtTotalPrice.Value > cardval)
                   {
                       MessageBox.Show("此卡中余额不足。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                       return;
                   }
                   VehicleGasCard card = cardlist[0];
                   card.TatalPrice = card.TatalPrice - txtTotalPrice.Value;
                   MainHelper.GetSqlMap<VehicleGasCard>().Update<VehicleGasCard>(card);
               }
            }
          
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void txtAct_Name_EditValueChanged(object sender, EventArgs e)
        {

        }

        private void txtAccountTitle_EditValueChanged(object sender, EventArgs e)
        {
            if (txtAccountTitle.Text != "")
            {
                rowData.AccountCode = ((Accounts)txtAccountTitle.Properties.GetDataSourceRowByKeyValue(txtAccountTitle.Text)).AccountCode;
                txtAccountCode.Text = rowData.AccountCode;
            }
        }

        private void txtVehicleNumber_EditValueChanged(object sender, EventArgs e)
        {
            if (txtVehicleNumber.Text != "")
            {
                rowData.VehicleID = ((Vehicles)txtVehicleNumber.Properties.GetDataSourceRowByKeyValue(txtVehicleNumber.Text)).VehicleID;
            }
        }

        private void lookGasCard_Num_EditValueChanged(object sender, EventArgs e)
        {
            if (lookGasCard_Num.Text != "")
            {
                rowData.GasCard_ID = ((VehicleGasCard)lookGasCard_Num.Properties.GetDataSourceRowByKeyValue(lookGasCard_Num.Text)).GasCard_ID;
            }
        }

        private void lookGasStation_EditValueChanged(object sender, EventArgs e)
        {
            if (lookGasStation.Text != "")
            {
                rowData.GasStation_ID = ((VehicleGasStation)lookGasStation.Properties.GetDataSourceRowByKeyValue(lookGasStation.Text)).GasStation_ID;
            }
        }

        private void txtEmployeeName_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            NameClick();
        }

        private void txtEmployeeName_Click(object sender, EventArgs e)
        {
            NameClick();
        }

        public void NameClick()
        {
            Ebada.Client.Platform.Selector.frmEmpSelectorByOrgInfo frm = new Ebada.Client.Platform.Selector.frmEmpSelectorByOrgInfo();
            frm.StartPosition = FormStartPosition.CenterScreen;
            frm.InitionData(string.Format(" where 1=1", this.txtEmployeeName.Text), false, false);
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtEmployeeName.Text = frm.CurrentEmp.Empolyee_Name;
                rowData.EmployeeID = frm.CurrentEmp.Empolyee_ID;
            }
        }

        private void txtVehicleNumber_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmVehiclesSeletcter frm = new frmVehiclesSeletcter();
            frm.InitData_Slect("");
            if (frm.ShowDialog() == DialogResult.OK)
            {
                txtVehicleNumber.Text = frm.SelectObj.VehicleNumber;
                txtVehicleNumber.ClosePopup();
            }
        }

        private void lookGasCard_Num_Properties_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            CardClick();
        }

        private void lookGasCard_Num_Properties_Click(object sender, EventArgs e)
        {
            CardClick();
        }
        public void CardClick()
        {
            frmCardSelector frm = new frmCardSelector();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                lookGasCard_Num.Text = frm.SelectObj.GasCard_Num;
                cardval = frm.SelectObj.TatalPrice;
                lookGasCard_Num.ClosePopup();
            }
        }

        private void txtNumber_ValueChanged(object sender, EventArgs e)
        {
            rowData.TotalPrice = txtPrice.Value * txtNumber.Value;
            txtTotalPrice.Value = txtPrice.Value * txtNumber.Value;
        }

        private void txtPrice_EditValueChanged(object sender, EventArgs e)
        {
            rowData.TotalPrice = txtPrice.Value * txtNumber.Value;
            txtTotalPrice.Value = txtPrice.Value * txtNumber.Value;
        }

        private void lookGasStation_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            StatClick();
        }

        private void lookGasStation_Click(object sender, EventArgs e)
        {
            StatClick();
        }
        public void StatClick()
        {
            frmStatSelector frm = new frmStatSelector();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                lookGasStation.Text = frm.SelectObj.GasStation;
                lookGasStation.ClosePopup();
            }
        }
    }
}